package com.xsmart.order.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.order.dao.OrderFillsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import javax.xml.ws.Response;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author pansheng
 * @date 2017/12/14
 */
@Service
public class OrderFillsService {

    @Autowired
    OrderFillsDao orderFillsDao;

    /**查询订单列表
     * @param requestBean
     * @return
     */
    public ResponseBean queryOrder(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        String orderSn = requestBean.getRequestParamString("order_sn");
        String orderStatus = requestBean.getRequestParamString("order_status");
        String payStatus = requestBean.getRequestParamString("pay_status");
        String shippingStatus = requestBean.getRequestParamString("shipping_status");
        String orderId=requestBean.getRequestParamString("order_id");
        String shopName=requestBean.getRequestParamString("shop_name");
        if(shopName!=null){
            shopName = shopName.trim();
        }
        String selectType=requestBean.getRequestParamString("selectType");
        String platform=requestBean.getRequestParamString("platForm");

        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(orderFillsDao.queryOrder(page, orderSn,orderStatus,payStatus,shippingStatus,orderId,shopName,selectType,platform));
        responseBean.addResultPageItem("orderManage", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**新建订单
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean addOrder(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        Map<String, Object> orderMap = getParam(requestBean);
        try{
            int num = orderFillsDao.addOrder(orderMap);
            responseBean.addResultPageItem("orderManage", page);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new  RuntimeException();

        }

        return responseBean;
    }


    /**  修改订单状态
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean modOrder(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String orderId = requestBean.getRequestParamString("order_id");
        String orderStatus = requestBean.getRequestParamString("order_status");
        String shipping_status = requestBean.getRequestParamString("shipping_status");
        Map<String, Object> orderMap = new HashMap<>();
        orderMap.put("order_status",orderStatus);
        orderMap.put("shipping_status",shipping_status);
        try{
            int num = orderFillsDao.modOrder(orderMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }

    /**
     * 查询订单评价
     * @param requestBean
     * @return
     */
    public ResponseBean queryOgaText(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        Integer orderId=requestBean.getRequestParamInteger("order_id");
        List<Map<String,Object>> ogaText=orderFillsDao.queryOgaText(orderId);
        responseBean.addResultItem("ogaText",ogaText);
        responseBean.addResultItem("optionInfo",orderFillsDao.queryOptionInfo(orderId));
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }


    /**查询订单详情
     * @param requestBean
     * @return
     */
    public ResponseBean queryOrderDetail(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 5);
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        Integer orderId = requestBean.getRequestParamInteger("order_id");
        page.setRecords(orderFillsDao.queryGoods(page,orderId));
        responseBean.addResultPageItem("orderManage", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**获取订单参数
     * @param requestBean
     * @return
     */
    private Map<String, Object> getParam(RequestBean requestBean) {

        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sfDate.format(new Date());
        String user_id = requestBean.getRequestParamString("user_id");
        Map<String, Object> orderMap = new HashMap<>();
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
        String order_sn = sfDate.format(new Date())+rannum;
        String[][] params = new String[][]{

                {"user_id", ""},
                {"shipping_id", "1"},
                {"pay_id", "1"},
                {"order_sn", ""},
                {"order_status", "0"},
                {"shipping_status", "0"},
                {"pay_status", "0"},
                {"postscript", "1"},
                {"goods_amount", "1"},

                {"shipping_fee", "1"},

                {"money_paid", "0.000"},
                {"integral", "1"},
                {"integral_money", "0"},
                {"bonus", "0"},
                {"order_amount", "0"},
                {"add_time", strDate},
                {"confirm_time", ""},
                {"pay_time", ""},
                {"shipping_time","" }
        };

        for (String[] param : params) {
            Object value = requestBean.getRequestParam(param[0]);
            if (value == null || "".equals(value)) {
                orderMap.put(param[0], param[1]);
            } else {
                orderMap.put(param[0], requestBean.getRequestParam(param[0]));
            }
        }

        orderMap.put("user_id",user_id);
        orderMap.put("order_sn",order_sn);

        return orderMap;
    }

    /**
     * 查询订单评价
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean changeStatus(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        String shippingStatus=requestBean.getRequestParamString("shipping_status");
        String []shippingArray = new String[]{"未发货","已发货","退货","已收货","备货中"};
        String []payArray = new String[]{"未付款","付款中","已付款"};
        String []orderArray = new String[]{"未确认","确认","已取消","无效","退货"};
        if(shippingArray[0].equals(shippingStatus)){
            shippingStatus=String.valueOf(0);
        }else if(shippingArray[1].equals(shippingStatus)){
            shippingStatus=String.valueOf(1);
        }else if(shippingArray[2].equals(shippingStatus)){
            shippingStatus=String.valueOf(2);
        }else if(shippingArray[3].equals(shippingStatus)){
            shippingStatus=String.valueOf(3);
        }else if(shippingArray[4].equals(shippingStatus)){
            shippingStatus=String.valueOf(4);
        }
        String orderStatus=requestBean.getRequestParamString("order_status");
        if(orderArray[0].equals(orderStatus)){
            orderStatus=String.valueOf(0);
        }else if(orderArray[1].equals(orderStatus)){
            orderStatus=String.valueOf(1);
        }else if(orderArray[2].equals(orderStatus)){
            orderStatus=String.valueOf(2);
        }else if(orderArray[3].equals(orderStatus)){
            orderStatus=String.valueOf(3);
        }else if(orderArray[4].equals(orderStatus)){
            orderStatus=String.valueOf(4);
        }
        Integer orderId=requestBean.getRequestParamInteger("order_id");
        Integer userId=requestBean.getRequestParamInteger("user_id");
        String payStatus=requestBean.getRequestParamString("pay_status");
        payStatus =payStatus.trim();
        if(payArray[0].equals(payStatus)){
            payStatus=String.valueOf(0);
        }else if(payArray[1].equals(payStatus)){
            payStatus=String.valueOf(1);
        }else if(payArray[2].equals(payStatus)){
            payStatus=String.valueOf(2);
        }
        String actionNote=requestBean.getRequestParamString("action_note");
        Map<String,Object>optionInfo=new HashMap<String,Object>();
        optionInfo.put("userId",userId);
        optionInfo.put("orderId",orderId);
        optionInfo.put("shippingStatus",shippingStatus);
        optionInfo.put("orderStatus",orderStatus);
        optionInfo.put("payStatus",payStatus);
        optionInfo.put("actionNote",actionNote);

        try{
            int flag=orderFillsDao.updateOrderStatus(orderStatus,shippingStatus,orderId);
            orderFillsDao.insertOrderAction(optionInfo);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        return responseBean;
    }
}




